package com.foreknow.elm.service.Impl;


import com.foreknow.elm.dao.Impl.UserDaoImpl;
import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    @Override
    public User getUserByIdByPass(String userId,String password) {
        User user = new User();
        UserDao userDao = new UserDaoImpl();
        try {
            user = userDao.getUserByIdByPass(userId,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return user;
    }

    @Override
    public Integer getUserById(String userId) {
        UserDao userDao= new UserDaoImpl();
        Integer result = 0;
        try {
            DBUtil.beginTransaction();//开启一个事务
            result = userDao.getUserById(userId);
            DBUtil.commitTransaction();//提交一个事务
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();  //回滚一个事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }

    @Override
    public Integer saveUser(String userId, String password, String userName, Integer userSex) {
        UserDao userDao= new UserDaoImpl();
        Integer result = 0;
        try {
            DBUtil.beginTransaction();//开启一个事务
            result = userDao. saveUser(userId,password,userName,userSex);
            DBUtil.commitTransaction();//提交一个事务
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();  //回滚一个事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }
}